package com.icss.ah.distributionPerson.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 配送人员维护Action
 * @author wangjun
 *
 */
@Controller
@Scope("prototype")
public class DistributionPersonAction  extends BaseAction{
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	private String operat;			//操作标识
	private String userUuid;		//人员id
	private String userAccount;		//人员帐号
	private String userName;		//人员姓名
	private String userPassword;	//账号密码
	private String userTel;			//手机号码
	private String sortNum;			//排序
	private String rePassword = ResourceLoader.getInstance("icss").getString("rePassword");
	/**
	 * 进入配送人员列表界面
	 * @return
	 */
	public String toDistributionPersonList(){
		request = this.getRequest();
		return "toDistributionPersonList";
	}
	/**
	 * 查询配送人员列表
	 */
	public void queryDistributionPersonList(){
		request = this.getRequest();
		String result = "";
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   	//获取店铺id 
			String userAccount = request.getParameter("userAccount");//人员帐号（查询条件）
			userAccount = userAccount == null ? "" : userAccount;
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",getBaseCorpId());
			map.put("shopId",shopId);
			map.put("userAccount",userAccount);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> vehicleList = new ArrayList();
			vehicleList = distributionPersonService.queryDistributionPersonList(map);	
			long vehicleCount = distributionPersonService.queryDistributionPersonCount(map);//查询总记录数
			dataGridJson.setRows(vehicleList);
			dataGridJson.setTotal(vehicleCount); 
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryDistributionPersonList|查询配送人员信息列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryDistributionPersonList|查询配送人员信息列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 进入编辑配送人员信息界面
	 * @return
	 */
	public String editPerson(){
		request = this.getRequest();
		String userUuid = request.getParameter("userUuid");
		userUuid = userUuid == null ? "" : userUuid;
		HashMap map = new HashMap();
		map.put("operat", operat);
		Map personMap = new HashMap();
		if("edit".equals(operat)){
			map.put("userUuid", userUuid);
			personMap = distributionPersonService.queryPersonMap(map);
		}
		request.setAttribute("personMap", personMap);
		request.setAttribute("operat", operat);
		return "toDistributionPersonEdit";
	}
	
	/**
	 * 保存配送人员信息操作(添加或修改)
	 * @return
	 */
	public String savePerson() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   	//获取店铺id 
			HashMap map = new HashMap();
			map.put("userUuid",userUuid );
			map.put("userAccount", userAccount);
			map.put("userName",userName );
			map.put("userPassword",MD5Util.toMD5(userPassword) );
			map.put("userTel", userTel);
			map.put("sortNum",sortNum );
			map.put("corpId", getBaseCorpId());
			map.put("shopId",shopId );
			boolean revl = false;
			revl =  distributionPersonService.savePerson(map,userUuid,operat);
			if(revl){
				jsonText.setMsg("保存成功！");
				jsonText.setSuccess(true);
			}else{
				jsonText.setMsg("保存失败！");
				jsonText.setSuccess(false);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "savePerson";
	}
	
	/**
	 * 删除配送人员
	 * @return
	 */
	public String deletePerson(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userUuid = request.getParameter("userUuid");
			userUuid = userUuid == null ? "" : userUuid;
			HashMap map = new HashMap();
			map.put("userUuid", userUuid);
			boolean revl = false;
			revl = distributionPersonService.deletePerson(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("删除成功！");
		} catch (Exception e) {
			jsonText.setMsg("删除失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 停用启用操作
	 * @return
	 */
	public String dealStatus(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userUuid = request.getParameter("userUuid");//车辆id
			userUuid = userUuid == null ? "" : userUuid;
			String status = request.getParameter("status");//停用或启用状态
			status = status == null ? "" : status;
			HashMap map = new HashMap();
			map.put("userUuid", userUuid);
			map.put("status", status);
			boolean revl = false;
			revl = distributionPersonService.dealStatus(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg("操作成功！");
			} else {
				jsonText.setMsg("操作失败！");
			}
		} catch (Exception e) {
			jsonText.setMsg("操作失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 重置密码
	 * @return
	 */
	public String resetPassword(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String userUuid = request.getParameter("userUuid");//车辆id
			userUuid = userUuid == null ? "" : userUuid;
			HashMap map = new HashMap();
			map.put("userUuid", userUuid);
			map.put("rePassword", MD5Util.toMD5(rePassword));
			boolean revl = false;
			revl = distributionPersonService.resetPassword(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg(rePassword);
			} else {
				jsonText.setMsg("密码重置失败！");
			}
		} catch (Exception e) {
			jsonText.setMsg("密码重置失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	/**
	 * 验证账号是否存在
	 * @return
	 */
	public String checkUserAccountExist(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			HashMap map = new HashMap();
			map.put("userAccount", userAccount);
			boolean revl = false;
			revl = distributionPersonService.checkUserAccountExist(map);
			jsonText.setSuccess(revl);
			if(revl){
				jsonText.setMsg("验证通过");
			} else {
				jsonText.setMsg("账号已存在");
			}
		} catch (Exception e) {
			jsonText.setMsg("验证失败");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	public String getOperat() {
		return operat;
	}
	public void setOperat(String operat) {
		this.operat = operat;
	}
	public String getUserUuid() {
		return userUuid;
	}
	public void setUserUuid(String userUuid) {
		this.userUuid = userUuid;
	}
	public String getUserAccount() {
		return userAccount;
	}
	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserTel() {
		return userTel;
	}
	public void setUserTel(String userTel) {
		this.userTel = userTel;
	}
	public String getSortNum() {
		return sortNum;
	}
	public void setSortNum(String sortNum) {
		this.sortNum = sortNum;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public String getRePassword() {
		return rePassword;
	}
	

}
